package www.qiyue.wbx;

import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.text.SimpleDateFormat;
import java.util.*;

@RestController
public class wbxtest {

    @Autowired
    private JdbcTemplate jdbcTemplate;
    private int parseInt;

    @RequestMapping("wbxtest")
    public void demo(){
        String sql ="SELECT * from ais_contract WHERE contract_code is NULL and sys_org_code='A01A04'";
        List<Map<String, Object>> mapList = jdbcTemplate.queryForList(sql);
        for (Map<String, Object> map : mapList) {
            String company_id = map.get("company_id") == null ? "1" : map.get("company_id").toString();
            String rent_unit_id = map.get("rent_unit_id") == null ? "1" : map.get("rent_unit_id").toString();
            String id = map.get("id") == null ? "1" : map.get("id").toString();
            if (!("1".equals(company_id)||"1".equals(rent_unit_id)||"1".equals(id))){
                String s = test1(company_id, rent_unit_id);
                String updatesql = "UPDATE ais_contract set contract_code='"+s+"' WHERE id='"+id+"'";
                jdbcTemplate.update(updatesql);
                System.out.println("s = " + s);
            }

        }
    }


    public String test1(String conparyID,String managementID)  {

        Map<String, Object> map = new HashMap<>();
        // 获取当前年份
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy");
        String format = sdf.format(new Date());
        // 从数据库ais_company获取abbreviated_name公司名缩写字段
        String sql = "select abbreviated_name from ais_company WHERE id = '" + conparyID + "'";
        String SqlConparyName = null;
        try {
            SqlConparyName = jdbcTemplate.queryForObject(sql, String.class);
        } catch (Exception e) {
            map.put("code", "");

        }

        // 获取后4位编码最大值
        String sqlMax = "select MAX(SUBSTR(a.contract_code FROM LENGTH(a.contract_code)-3 FOR LENGTH(a.contract_code))) as maxCode from ais_contract a where contract_code like '%"+format+"%' and contract_code like 'W%'";
        Map<String, Object> maxMap = new HashMap<>();
        maxMap = jdbcTemplate.queryForMap(sqlMax);
        String queryForObject = (String) maxMap.get("maxCode");
        if (StringUtils.isNotBlank(queryForObject)) {
            if (StringUtils.isNumeric(queryForObject)) {
                parseInt = Integer.parseInt(queryForObject);
                ++parseInt;
            } else {
                parseInt = 1;
            }
        } else {
            parseInt = 1;
        }
        // 从数据库ais_houseproject 获取项目的缩写字段abbreviated_item_name
        String sqls = "select c.abbreviated_item_name from ais_rent_unit a ,ais_house b ,ais_houseproject c where a.house_id =b.id and b.project = c.`code` and a.id ='"
                + managementID + "'";
        List<Map<String,Object>> list =new ArrayList<>();
        list = jdbcTemplate.queryForList(sqls);
        if(list!=null && list.size()>0) {
            String SqlManagementName = (String) list.get(0).get("abbreviated_item_name");
            if(StringUtils.isEmpty(SqlManagementName)) {

            }else {
                // 设置最后4位
                map.put("code", "W" + SqlConparyName + "-" + SqlManagementName + "-" + format + "-" +  String.format("%04d", parseInt));

            }
        }

        return map.get("code")+"";
    }
}
